
#include "init.h"

////////////////////////////////////////////////////////////////////////////////
void bman_net::draw_connect( int key )
{
    if(key)  return;

    static int binit=0;
    static TexImage backg;

    if(!binit) {
        backg.create( (World::XTILES - 4)*TWpx, (World::YTILES-2)*TWpx, TARGET_BPP, false );
        SDL_FillRect( backg._surf, 0, 0 );
        SDL_SetAlpha( backg._surf, SDL_SRCALPHA, 128 );

        binit = 1;
    }

    SCREEN.draw( backg, 2*TWpx, TWpx );
    //draw title
    {
        const char* t = 0;
        if( !is_connected() )
            t = "Not connected";
        else if( CW.gs.state == World::GS_GAME_WAIT )
            t = "Waiting for players to connect";
        else if( CW.gs.state == World::GS_GAME_START )
            t = "Starting game";
        else if( CW.gs.state == World::GS_ROUND_PREPARE )
            t = "Preparing the next round";
        else if( CW.gs.state == World::GS_ROUND_END )
            t = "Round ended";
        else if( CW.gs.state == World::GS_ROUND_END_DRAW )
            t = "Draw game";
        else if( CW.gs.state == World::GS_ROUND_NOT_ENOUGH_PLAYERS )
            t = "Not enough players, waiting for reconnects";
        else if( CW.gs.state == World::GS_GAME_END )
            t = "Game over";
        else if( CW.gs.state == World::GS_GAME_END_SCRATCH )
            t = "Game scratched";

        if(!t)  return;

        uint w = FONT.text_width(t);
        FONT.print( SCREEN, 2*TWpx + (backg.width() - w)/2, 3*TWpx/2, t );
    }

    uint x = 3*TWpx;
    uint y = 3*TWpx;

    uint lim = CW._men.size();
    for( uint i=0; i<lim; ++i, y+=TWpx )
    {
        man& m = *CW._men[i];
        if( m._nick.is_empty() )  continue;

        if( m.is_connected() )
            SCREEN.draw_colorize( CW._spr_bomb._array[0], 0, m.get_colorpri(), m.get_colorsec(), x, y );

        FONT.print( SCREEN, x+3*TWpx/2, y+TWpx/4, m._nick );

        charstr n;
        n.append_num( 10, m._nwins, 3, charstr::ALIGN_NUM_RIGHT );
        n << "      ";
        n.append_num( 10, int(m._nkills-m._nsuicides), 3, charstr::ALIGN_NUM_RIGHT );

        FONT.print( SCREEN, x+10*TWpx, y+TWpx/4, n );
    }
}

